﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.LuFang.API.Application.Command.MenuCommand;
using RBAC.LuFang.API.Dtos;
using RBAC.LuFang.Domain;
using RBAC.LuFang.Domain.DTO;
using RBAC.LuFang.Domin.Services;
using System.Collections.Generic;

namespace RBAC.LuFang.API.Controllers
{
    /// <summary>
    /// 菜单控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenuController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IMenuService _menuService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public MenuController(IMediator mediator, IMenuService menuService)
        {
            _mediator = mediator;
            _menuService = menuService;
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMenu(MenuCreateCommand menu)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
               result.Data=await _mediator.Send(menu);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示父级菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MenuInfo>>> GetMenuPid()
        {
            ApiResult <List<MenuInfo>> result= new ApiResult<List<MenuInfo>> ();
            try
            {
                result.Data = await _mediator.Send(new MenuPidSelectCommand());
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]

        public async Task<ApiResult<Pagese>> GetMenu([FromQuery]MenuSelectCommand menu)
        {
            ApiResult<Pagese> result = new ApiResult<Pagese>(); 
            try
            {
                result.Data = await _mediator.Send(menu);
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 验证是否有子集
        /// </summary>
        /// <param name="menuid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<int>> Yan(int menuid)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _menuService.Yan(menuid);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 菜单删除
        /// </summary>
        /// <param name="menuid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> MenuDel(MenuDeleteCommand menuid)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(menuid);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="menuid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<MenuInfo>> GetByid(MenuFanCommand menuid)
        {
            ApiResult<MenuInfo> result = new ApiResult<MenuInfo>();
            try
            {
                result.Data = await _mediator.Send(menuid);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdMenu(MenuUpdateCommand menu)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(menu);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }







    }
}
